<?php
/**
 * User: @lose
 * Date: 2019-10-10
 * Time: 18:00
 */

namespace Biz\model\store;

use Doraemon\pockets\datebase\BaseDbModel;

class StoreOrder extends BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'store_paid_order';

    const WX_METHOD = 1;
    const ZHIFUBAO_METHOD = 2;//支付宝
    const BANK_METHOD = 3;//银行转账

    const TRANSFER_METHOD = [
         self::WX_METHOD => '微信转账',
         self::ZHIFUBAO_METHOD => '支付宝转账',
         self::BANK_METHOD => "银行转账"
    ];


    const HAVE_PAID = 1;//已支付
    const HAVE_NOT_PAID =2;//未支付
    const PAID_FAIL = 3;//支付失败


    public static function getOrderListByWhere($where , $limit)
    {
        $condition['LIMIT'] = $limit;
        if($where['start_time'] && $where['end_time']){
            $condition['AND'][self::_TABLE_NAME. ".pay_time[<=]"] = $where['end_time'];
            $condition['AND'][ self::_TABLE_NAME . ".pay_time[>=]"] = $where['start_time'];
        }
        if($where['store_name'] ){
            $condition['AND'][StoreAccount::_TABLE_NAME. ".store_name"] = $where['store_name'];
        }
        if($where['payment_method'] ){
            $condition['AND'][self::_TABLE_NAME. ".payment_method"] = $where['payment_method'];
        }
        if($where['server_type'] ){
            $condition['AND'][StoreAccount::_TABLE_NAME. ".server_type"] = $where['server_type'];
        }

        if($where['store_id'] ){
            $condition['AND'][StoreAccount::_TABLE_NAME. ".store_id"] = $where['store_id'];
        }

        $condition['ORDER'] =  [self::_TABLE_NAME. ".addtime" => 'DESC'];

        return self::select(
            [
                "[>]" . StoreAccount::_TABLE_NAME => ["account_id" => "id"],
            ],
            [
                self::_TABLE_NAME . ".id",
                self::_TABLE_NAME . ".pay_time",
                self::_TABLE_NAME . ".transact_serial_num",
                self::_TABLE_NAME . ".payment_amount",
                self::_TABLE_NAME . ".payment_method",
                self::_TABLE_NAME . ".admin_id",
                self::_TABLE_NAME . ".auth_start_time",
                self::_TABLE_NAME . ".auth_end_time",
                StoreAccount::_TABLE_NAME . ".store_name",
                StoreAccount::_TABLE_NAME . ".server_type",
                StoreAccount::_TABLE_NAME . ".store_id",
            ],$condition
        );
    }




    public static function getOrderCountByWhere($where)
    {
        $condition = [];
        if($where['start_time'] && $where['end_time']){
            $condition['AND'][self::_TABLE_NAME. ".pay_time[<=]"] = $where['end_time'];
            $condition['AND'][ self::_TABLE_NAME . ".pay_time[>=]"] = $where['start_time'];
        }
        if($where['store_name'] ){
            $condition['AND'][StoreAccount::_TABLE_NAME. ".store_name"] = $where['store_name'];
        }
        if($where['payment_method'] ){
            $condition['AND'][self::_TABLE_NAME. ".payment_method"] = $where['payment_method'];
        }
        if($where['store_id'] ){
            $condition['AND'][StoreAccount::_TABLE_NAME. ".store_id"] = $where['store_id'];
        }

        return self::count(
            [
                "[>]" . StoreAccount::_TABLE_NAME => ["account_id" => "id"],
            ],
            [
                self::_TABLE_NAME . ".id",
            ],$condition
        );

    }









    public static function getListByWhere($where, $field = '*')
    {
        return self::select($field, $where);
    }

    public static function getListByAdmin($where, $limit)
    {
        return self::select('*', [
            'AND' => $where,
            'LIMIT' => $limit
        ]);
    }

    public static function add($data)
    {
        $result = self::insert($data)->rowCount();
        if($result) {
            return self::id();
        }

        return false;
    }

    public static function updateById($id, $data)
    {
        $result = self::update($data, ['id' => $id])->rowCount();
        if($result) {
            return $id;
        }

        return false;
    }

    public static function getInfo($id)
    {
        return self::get('*', ['id' => $id]);
    }

    public static function getCount($where)
    {
        return self::count($where);
    }


    public static function getInfoByAid($acctoun_id)
    {
        return self::get("*" ,['account_id' => $acctoun_id]);
    }



}